﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Markup;
using System.Xml;
using AppDesigner.XamlDom;
using System.IO;

namespace AppDesignerTest
{
    public class TestHelper
    {
        public static void TestLoadingXaml(string xaml)
        {

            //using custom xamlparser parse xaml
            Debug.WriteLine("Load using custom xamlparser");
            ExampleClass.nextUniqueIndex = 0;
            LogHelper.LogBuilder = new StringBuilder();

            //Parse generate the xamldocument
            XamlDocument doc = XamlParser.Instance.Parse(new XmlTextReader(new StringReader(xaml)));
            Assert.IsNotNull(doc, "doc is null.");
            object ownResult = doc.RootInstance;
            string ownLog = LogHelper.LogBuilder.ToString();
            Assert.IsNotNull(ownResult, "ownResult is null.");

            //using xamlreader parse xaml
            Debug.WriteLine("Load using builtin XamlReader.");
            ExampleClass.nextUniqueIndex = 0;
            LogHelper.LogBuilder = new StringBuilder();
            object officalResult = XamlReader.Load(new XmlTextReader(new StringReader(xaml)));
            string officalLog = LogHelper.LogBuilder.ToString();
            Assert.IsNotNull(officalResult, "officalResult is null.");


            //reset
            LogHelper.LogBuilder = null;

            //Compare generate object
            string officalSaved = XamlWriter.Save(officalResult);
            string ownSaved = XamlWriter.Save(ownResult);

            Debug.WriteLine("OfficalSaved:");
            Debug.WriteLine(officalSaved);
            Debug.WriteLine("OwnSaved:");
            Debug.WriteLine(ownSaved);

            Assert.AreEqual(officalSaved, ownSaved);

            //Compare generate log
            Debug.WriteLine("OfficalLog:");
            Debug.WriteLine(officalLog);
            Debug.WriteLine("OwnLog:");
            Debug.WriteLine(ownLog);

            Assert.AreEqual(officalLog, ownLog);
        }
    }
}
